<%--
	author:chengd
	time:2012/5/12 18:06:10
--%>



<%@page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib uri="TowkerTagLib" prefix="rbac"%>
<%@ taglib uri="/WEB-INF/tags/CommonFunction.tld" prefix="cfn" %>
<%@include file="/commons/include/html_doctype.html"%>
<html>
<head>
	<title>添加</title>
	<%@include file="/commons/include/add.jsp" %>
    <script type="text/javascript" src="${ctx}/js/calendar/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript">
$(function(){
	$("#dataFormSave").unbind("click");
	$("#dataFormSave").click(function()
	{
		$('input[name*="amount"]').each(function(){
			$(this).rules("add", {
						  required: true,
						  number:true ,
						  min:1,
						  messages: { required: "请填写", number:"请填写数字"}
						  });
		});
		$("#dataForm").submit();
	});
	jQuery("#dataForm").validate(
	{
		rules: {
			custom_code : {
				required: true
			},
			create_date : {
				required: true
			}
		},
		messages: {
		},
		submitHandler:function(form)
		{
			setGoodsInfo();
			jQuery(form).ajaxSubmit(_options);
		}
	});
});
_callback = function() {
	location.href = ctx + "/manage/sell/getSellPlanApp.htm";
}
validCallBack = function()
{
	if($("#goodsInfo").val() == "") {
		alert("请选择产品!");
		return false;
	}
	return true;
};
function setGoodsInfo() {
	var goodsInfoStr = "";
	//添加页面自定义校验
	for(var id in goods) {
		goodsInfoStr += id + ",";
	}
	//alert(goodsInfoStr);
	$("#goodsInfo").attr("value", goodsInfoStr);
}
var goods = {};
//JavaScript Document
function selectGoods() {
	//showModelessDialog
	//encodeURI、encodeURIComponent
	var source = encodeURIComponent(ctx+"/manage/basicData/selectGoods.htm?typeid=&classId=&r=" + Math.random());
	var url = ctx + "/commons/showDialog.jsp?r=" + Math.random() + "&url=" + source;
	var result = window.showModalDialog(url, this, "dialogWidth=600px;dialogHeight=600px;scroll=no");
	var panel = $("#MGoodsPanel");
	var html = "";
	for(var i = 0; i < result.length; i++) {
		var v = result[i];
		if(typeof(goods[v.id]) == "undefined" || goods[v.id] == null) {
			goods[v.id] = v.sn;
			html += '    <tbody><tr>'
				+ '      <td>' + v.sn + '<input type="hidden" name="wprice_' + v.id + '" id="wprice_' + v.id + '" value="' + v.wprice + '"  />'
				
				+ '      <input name="discount_' + v.id + '" type="hidden" id="discount_' + v.id + '" value="100" size="10"  />'
				+ '      <input name="sprice_' + v.id + '" type="hidden" id="sprice_' + v.id + '" size="10">'
				+ '      <input name="sumMoney_' + v.id + '" type="hidden" id="sumMoney_' + v.id + '" size="10">'
				
				+ '</td>'
				+ '      <td>' + v.name + '</td>'
				+ '      <td><input name="amount_' + v.id + '" type="text" id="amount_' + v.id + '" value="0" size="10"  onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')" /></td>';
				
				html += '      <td>' + v.weight + '</td>'
				+ '      <td>' + v.unit + '</td>'
				+ '      <td><a href="#" onClick="deleteGoods(this, ' + v.id + ')">删除</a></td>'
				+ '    </tr>';
		}
	}
	html = html+'</tbody>';
	panel.append(html);
}
function deleteGoods(el, id) {
	var row = $(el).parent().parent();
	row.remove();
	goods[id] = null;
	delete goods[id];
}
function calcGoodsTotalM(el, id) {	
	var obj = $(el);
	var prefix = obj.attr("name").split('_')[0];
	var sprice = $("#sprice_"+id).val();
	var discount = $("#discount_"+id).val();
	var sumMoney = $("#sumMoney_"+id).val();
	var wprice = $("#wprice_"+id).val();
	var amount = $("#amount_"+id).val();
	if(prefix == "discount" || prefix == "amount") {
		$("#sprice_"+id).attr("value", discount * wprice / 100);
		$("#sumMoney_"+id).attr("value",(discount * wprice / 100 * amount).toFixedU(2));
	} else if(prefix == "sprice") {
		$("#discount_"+id).attr("value", parseInt(sprice / wprice * 100));
		$("#sumMoney_"+id).attr("value", (sprice * amount).toFixedU(2));
	} else {
		$("#discount_"+id).attr("value", parseInt((sumMoney / amount).toFixedU(2) / wprice * 100));
		$("#sprice_"+id).attr("value", (sumMoney / amount).toFixedU(2));
	}
	
}
	</script>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" class="listLogo">
	<tr>
		<td class="title">生产计划单添加</td>
		<td>
			<div class="menuTool">
				<a class="save" id="dataFormSave" href="#">保存</a>
				<a class="back" href="getSellPlanApp.htm">返回</a>
			</div>
		</td>
	</tr>
</table>
<div class="line"></div>
<form id="dataForm" method="post" action="addSellPlanApp2.htm">
<input type="hidden" name="goodsInfo" id="goodsInfo" value="" />
<table border="0" cellspacing="1" cellpadding="0" class="listTable">
	<tr>
		<td class="form_title">计划单号：</td>
		<td class="form_input">${app_code}</td>
	
		<td class="form_title">计划性质：</td>
		<td class="form_input"><select name="app_type" id="app_type">
		  <option value="1">销售</option>
          <option value="2">库存</option>
          <option value="3">固定</option>
          <option value="4">临时</option>
	    </select></td>
	</tr>
	<tr>
		<td class="form_title">客户代码：</td>
		<td class="form_input"><input type="text" id="custom_code" name="custom_code" value="" /></td>
	
		<td class="form_title">制单日期：</td>
		<td class="form_input"><input type="text" id="create_date" name="create_date" value="" readonly="readonly" class="Wdate" onFocus="WdatePicker({isShowWeek:true})" /></td>
	</tr>
	<tr>
		<td class="form_title">备注：</td>
		<td class="form_input" colspan="3"><textarea name="remark" style="width:99%" rows="5" id="remark"></textarea></td>
	</tr>
     <tr>
	  <td class="form_title">产品报价</td>
	  <td class="form_input" colspan="3"><table width="340" border="0"
						cellpadding="2" cellspacing="1" class="listLogo" id="MGoodsPanel">
						<tr>
							<td>产品代码</td>
							<td>产品名称</td>
							<td>产品数量</td>
							<td>重量</td>
							<td>计量单位</td>
								<td><input type="button" name="button" id="button"
									value="增加产品" onClick="selectGoods()">
								</td>
						</tr>
					</table></td>
    </tr>
</table>
</form>
</body>
</html>
